﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Billing.Win
{
    public partial class ClientsMainForm : Billing.WindowsBase.FormBase
    {

        public ClientsMainForm()
        {
            InitializeComponent();
            this.Load += new EventHandler(ClientsMainForm_Load);
            clientsList.CurrentChanged += new EventHandler(clientsList_CurrentChanged);
            clientsList.ChangePage += new EventHandler(clientsList_ChangePage);
            this.ucBillingPhones1.CurrentChanged += new System.EventHandler(this.ucClientBillingPhones1_CurrentChanged);

        }

        void ClientsMainForm_Load(object sender, EventArgs e)
        {
            //DoRefresh();

            clientsList.ToolClick += new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(TBM_ToolClick);

        }

        protected override void OnRegister()
        {
            if (!this.DesignMode && this.Register)
            {
                Billing.FormRegister.RegisterForm(this, this.GetType().FullName + Guid.NewGuid().ToString());
            }
        }

        private void DoRefresh()
        {
            ucClientDetails.Initialize(null, null);
            ucClientAdd.Initialize(null, null);
            ucBillingPhones1.Initialize(null);
            ucClientPhoneActions1.Initialize(ClientPhoneActions.Move, null);
            ucClientPhoneActions2.Initialize(ClientPhoneActions.Remove, null);
            clientsSearch.Search();
        }

        bool initializing = false;

        void clientsList_ChangePage(object sender, EventArgs e)
        {
            clientsSearch.CurrentPageNumber = clientsList.PageNumber;
            clientsSearch.Search();
        }

        void clientsList_CurrentChanged(object sender, EventArgs e)
        {
            Billing.DataObjects.ClientsView client = clientsList.DataObject;

            initializing = true;
            ucClientDetails.Initialize(clientsList.BindingSource, client);
            ucClientPhoneActions1.Initialize(ClientPhoneActions.Move, client);
            ucClientPhoneActions2.Initialize(ClientPhoneActions.Remove, client);
            ucBillingPhones1.Initialize(client, clientsSearch.PhoneNumber);
            initializing = false;
        }

        private void ucClientBillingPhones1_CurrentChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == ucBillingPhones1 && !initializing)
            {
                Billing.DataObjects.ClientsView client = clientsList.DataObject;
                ucClientPhoneActions1.Initialize(ClientPhoneActions.Move, client, ucBillingPhones1.Account);
                ucClientPhoneActions2.Initialize(ClientPhoneActions.Remove, client, ucBillingPhones1.Account);
            }
        }

        private void TBM_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Refresh":    // es kodi ase ar unda darches
                    DoRefresh();
                    break;

                case "Edit":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    break;

                case "New":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    ucClientAdd.Initialize(clientsList.BindingSource, null);
                    break;

                case "Phones":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    break;

                case "Move":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    break;
                case "Remove":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    break;
                case "Search":    // es kodi ase ar unda darches
                    DM.ControlPanes[e.Tool.Key].Pin();
                    DM.ControlPanes[e.Tool.Key].Activate();
                    break;
                case "ClientInfo":
                    if (this.clientsList.DataObject == null)
                    {
                        ShowWarning("კლიენტი არ არის არჩეული");
                        return;
                    }
                    ClientInfoForm f = new ClientInfoForm();
                    f.ClientID = this.clientsList.DataObject.ID;
                    f.Search();
                    f.Show();
                    break;
                case "REP:ClientInvoices":
                    DoShowReport("/BillingReports/ClientInvoices");
                    break;
                case "REP:ClientAccountsLines":
                    DoShowReport("/BillingReports/ClientAccountsLines");
                    break;
                case "REP:ClientAccounts":
                    DoShowReport("/BillingReports/ClientAccounts");
                    break;
                case "REP:UnknownNumbers":
                    DoShowReport("/BillingReports/ClientAccounts", -1);
                    break;
            }

        }

        private void DoShowReport(string reportName, int clientID)
        {
            try
            {
                Billing.Reports.ReportForm df = new Billing.Reports.ReportForm(reportName);
                df.StartPosition = FormStartPosition.CenterScreen;
                df.Text = string.Format("{0} - {1}", clientID, reportName);
                df.PrepareForSelectDialogStyle();
                df.AddParameter("clientID", clientID.ToString(), false);
                df.LoadReport();
                df.Show();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }

        private void DoShowReport(string reportName)
        {
            if (clientsList == null || clientsList.DataObject == null)
            {
                ShowWarning("აირჩიეთ კლიენტი");
                return;
            }
            DoShowReport(reportName, clientsList.DataObject.ID);
        }

        private void ucClientsSearch1_SearchDone(object sender, EventArgs e)
        {
            clientsList.SearchStatistics = clientsSearch.SearchStatistics;
            clientsList.DataSource = clientsSearch.Results;
            ucClientAdd.Initialize(clientsList.BindingSource, null);
        }

        private void ucClientsSearch1_ClearFieldsDone(object sender, EventArgs e)
        {

        }

        private void ucClientsSearch1_CancelDone(object sender, EventArgs e)
        {

        }
    }
}
